package com.fhnew.app.ui.activity.userinfo.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.fhnew.app.BuildConfig;
import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.update_PwdBean;

import com.fhnew.app.util.Md5;
import com.fhnew.app.util.SYSOUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/12.
 */

public class SafecenterFragment extends Fragment {

    private EditText oldpass;
    private EditText newpass;
    private EditText newpass1;
    private Button passcommit;
    private EditText Withdrawal;
    private EditText Withdrawal1;
    private Button Withdrawalbt;
    private EditText Withdrawalchange;
    private EditText Withdrawalchange1;
    private Button Withdrawalcahngebt;
    private LinearLayout Withdrawallayout;
    private LinearLayout Withdrawalpasslayout;

    private void initView (View view) {
        oldpass = view.findViewById(R.id.oldpass);
        newpass = view.findViewById(R.id.newpass);
        newpass1 = view.findViewById(R.id.newpass1);
        passcommit = view.findViewById(R.id.passcommit);
        Withdrawal = view.findViewById(R.id.Withdrawal);
        Withdrawal1 = view.findViewById(R.id.Withdrawal1);
        Withdrawalbt = view.findViewById(R.id.Withdrawalbt);
        Withdrawalchange = view.findViewById(R.id.Withdrawalchange);
        Withdrawalchange1 = view.findViewById(R.id.Withdrawalchange1);
        Withdrawalcahngebt = view.findViewById(R.id.Withdrawalcahngebt);
        Withdrawallayout = view.findViewById(R.id.Withdrawallayout);
        Withdrawalpasslayout = view.findViewById(R.id.Withdrawalpasslayout);

        passcommit.setOnClickListener(onClickListener);
        Withdrawalbt.setOnClickListener(onClickListener);
        Withdrawalcahngebt.setOnClickListener(onClickListener);

    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.safecenterlayout, null);
        initView(container);
        return container;
    }

    @Override
    public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (BuildConfig.CASH) {
            Withdrawallayout.setVisibility(View.VISIBLE);
            Withdrawalpasslayout.setVisibility(View.VISIBLE);
            if (UserModel.user.hasFundPwd) {
                Withdrawallayout.setVisibility(View.GONE);
                Withdrawalpasslayout.setVisibility(View.VISIBLE);
            } else {
                Withdrawallayout.setVisibility(View.VISIBLE);
                Withdrawalpasslayout.setVisibility(View.GONE);
            }
        } else {
            Withdrawallayout.setVisibility(View.GONE);
            Withdrawalpasslayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume () {
        super.onResume();
        if (BuildConfig.CASH) {
            Withdrawallayout.setVisibility(View.VISIBLE);
            Withdrawalpasslayout.setVisibility(View.VISIBLE);
            if (UserModel.user.hasFundPwd) {
                Withdrawallayout.setVisibility(View.GONE);
            } else {
                Withdrawallayout.setVisibility(View.VISIBLE);
            }
        } else {
            Withdrawallayout.setVisibility(View.GONE);
            Withdrawalpasslayout.setVisibility(View.GONE);
        }
    }

    private View.OnClickListener onClickListener  = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.passcommit:
                    //修改登录密码
                    if (oldpass.getText().toString().isEmpty()) {
                        Toast.makeText(getContext(), "请输入登录密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (newpass.getText().toString().isEmpty()) {
                        Toast.makeText(getContext(), "请输入新登录密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (newpass1.length() < 6) {
                        Toast.makeText(getContext(), "请输入6位以上的密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (newpass1.getText().toString().isEmpty()) {
                        Toast.makeText(getContext(), "请输入新登录密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!newpass.getText().toString().trim().matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{6,}$")) {
                        Toast.makeText(getContext(), "您的密码不符合规定", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!newpass.getText().toString().equals(newpass1.getText().toString())) {
                        Toast.makeText(getContext(), "两次输入的密码请保持一致", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    conform();
                    break;
                case R.id.Withdrawalbt:
                    //绑定提款密码
                    if (Withdrawal.getText().toString().isEmpty()) {
                        Toast.makeText(getContext(), "请输入账户登录密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Withdrawal1.getText().toString().isEmpty()) {
                        Toast.makeText(getContext(), "请设置取款密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
//                if (!Withdrawal.getText().toString().equals(Withdrawal1.getText().toString())) {
//                    Toast.makeText(getContext(), "两次输入的密码请保持一致", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                    saveFundPwd();
                    break;
                case R.id.Withdrawalcahngebt:
                    //修改提款密码
                    if (Withdrawalchange.getText().toString().isEmpty()) {
                        Toast.makeText(getContext(), "请输入旧的提款密码", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Withdrawalchange1.getText().toString().isEmpty()) {
                        Toast.makeText(getContext(), "请输入新的提款密码", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    updateFundPwd();
                    break;
            }
        }
    };





    /**
     * 修改提款密码
     */
    private void updateFundPwd () {
        HttpJsonUtils.getHttpApiInstance()
                .updateFundPwd(Constants.getuseAdress() + "api/m/user/updateFundPwd", Md5.toMd5(Withdrawalchange.getText().toString().trim()),
                        Md5.toMd5(Withdrawalchange1.getText().toString().trim()), UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<update_PwdBean>() {
                    @Override
                    protected void onCustomNext (update_PwdBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                Withdrawalchange1.setText("");
                                Withdrawalchange.setText("");
                            } else {
                                Toast.makeText(getContext(), resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    /**
     * 设置取款密码
     */
    private void saveFundPwd () {
        HttpJsonUtils.getHttpApiInstance()
                .saveFundPwd(Constants.getuseAdress() + "api/m/user/saveFundPwd", Md5.toMd5(Withdrawal.getText().toString().trim()),
                        Md5.toMd5(Withdrawal1.getText().toString().trim()), UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<update_PwdBean>() {
                    @Override
                    protected void onCustomNext (update_PwdBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                Withdrawal.setText("");
                                Withdrawal1.setText("");
                                Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                UserModel.user.hasFundPwd = true;
                                Withdrawallayout.setVisibility(View.GONE);
                                Withdrawalpasslayout.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(getContext(), resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    /**
     * 修改登录密码
     */
    private void conform () {
        HttpJsonUtils.getHttpApiInstance()
                .get_updat_loginpwd(Constants.getuseAdress() + "api/m/user/updateMyPwd", Md5.toMd5(oldpass.getText().toString().trim()),
                        Md5.toMd5(newpass1.getText().toString().trim()), UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<update_PwdBean>() {
                    @Override
                    protected void onCustomNext (update_PwdBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                oldpass.setText("");
                                newpass.setText("");
                                newpass1.setText("");
                            } else {
                                Toast.makeText(getContext(), resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });

    }

}
